﻿Imports System.Drawing.Drawing2D
Imports System.Drawing
Imports System.Windows.Forms
Imports System.ComponentModel

<DefaultEvent("Click")>
<ToolboxBitmap(GetType(Button))>
Public Class glowButton
    Inherits UserControl

    Dim WithEvents t_in As New Timer
    Dim WithEvents t_out As New Timer

    Dim glowIndex As Integer
    Dim glowColor As Color = SystemColors.ActiveCaption

    Dim txt As String = "button"
    Public Property ButtonText As String
        Get
            Return txt
        End Get
        Set(value As String)
            txt = value
            Me.Invalidate()
        End Set
    End Property

    Dim fnt As New Font("Segoe UI", 10, FontStyle.Bold)
    Public Property ButtonFont As Font
        Get
            Return fnt
        End Get
        Set(value As Font)
            fnt = value
            Me.Invalidate()
        End Set
    End Property

    Dim showImg As Boolean = False
    Public Property ShowImage As Boolean
        Get
            Return showImg
        End Get
        Set(value As Boolean)
            showImg = value
            Me.Invalidate()
        End Set
    End Property

    Dim img As Image
    Public Property ButtonImage As Image
        Get
            Return img
        End Get
        Set(value As Image)
            img = value
            Me.Invalidate()
        End Set
    End Property

    Dim imgSize As Integer = 24
    Public Property ImageSize As Integer
        Get
            Return imgSize
        End Get
        Set(value As Integer)
            imgSize = value
            Me.Invalidate()
        End Set
    End Property

    Enum ButtonState
        Normal
        Down
    End Enum

    Dim s As ButtonState = ButtonState.Normal

    Public Sub New()
        Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint Or ControlStyles.ResizeRedraw Or ControlStyles.OptimizedDoubleBuffer, True)

        'Initialize t_in
        t_in.Interval = 15
        t_in.Enabled = True

        'Initialize t_out
        t_out.Interval = 15
        t_out.Enabled = True

        'Setting control size
        Me.Size = New Size(80, 30)
    End Sub

    Private Sub t_in_Tick(sender As Object, e As EventArgs) Handles t_in.Tick
        If glowIndex = 255 Then
            t_in.Stop()
        Else
            glowIndex += 15
            Me.Invalidate()
        End If
    End Sub

    Private Sub t_out_Tick(sender As Object, e As EventArgs) Handles t_out.Tick
        If glowIndex = 0 Then
            t_out.Stop()
        Else
            glowIndex -= 15
            Me.Invalidate()
        End If
    End Sub

    Protected Overrides Sub OnMouseEnter(e As EventArgs)
        MyBase.OnMouseEnter(e)
        t_in.Start()
        t_out.Stop()
    End Sub

    Protected Overrides Sub OnMouseLeave(e As EventArgs)
        MyBase.OnMouseLeave(e)
        t_out.Start()
        t_in.Stop()

    End Sub

    Protected Overrides Sub OnMouseDown(e As MouseEventArgs)
        MyBase.OnMouseDown(e)
        t_in.Stop()
        t_out.Stop()

        s = ButtonState.Down
        Me.Invalidate()
    End Sub

    Protected Overrides Sub OnMouseUp(e As MouseEventArgs)
        MyBase.OnMouseUp(e)
        t_in.Start()
        t_out.Stop()

        s = ButtonState.Normal
        Me.Invalidate()
    End Sub

    Protected Overrides Sub OnPaint(e As PaintEventArgs)
        MyBase.OnPaint(e)
        Dim g As Graphics = e.Graphics

        g.SmoothingMode = SmoothingMode.AntiAlias
        g.InterpolationMode = InterpolationMode.HighQualityBicubic

        Dim base_rect As Rectangle = Me.ClientRectangle

        Dim brush_normal As New SolidBrush(SystemColors.ControlLight)
        Dim brush_glow As New SolidBrush(Color.FromArgb(glowIndex, glowColor))
        Dim brush_down As New SolidBrush(Color.FromArgb(60, Color.Black))
        Dim brush_hover As New SolidBrush(glowColor)

        If s = ButtonState.Normal Then
            g.FillRectangle(brush_normal, base_rect)
            g.FillRectangle(brush_glow, base_rect)

            If showImg = True Then
                Dim rctimg As New Rectangle(base_rect.X + (base_rect.Width / 2) - (imgSize / 2), base_rect.Y + (base_rect.Height / 2) - (imgSize / 2), imgSize, imgSize)

                If img IsNot Nothing Then
                    g.DrawImage(img, rctimg)
                End If
            Else
                g.DrawString(txt.ToUpper, fnt, New SolidBrush(Color.White), base_rect, New StringFormat With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center})
            End If

            g.DrawRectangle(New Pen(brush_normal, 1), base_rect)
            g.DrawRectangle(New Pen(brush_glow, 1), base_rect)

        Else
            g.FillRectangle(brush_hover, base_rect)
            g.FillRectangle(brush_down, base_rect)

            If showImg = True Then
                Dim rctimg As New Rectangle(base_rect.X + (base_rect.Width / 2) - (imgSize / 2), base_rect.Y + (base_rect.Height / 2) - (imgSize / 2), imgSize, imgSize)

                If img IsNot Nothing Then
                    g.DrawImage(img, rctimg)
                End If
            Else
                g.DrawString(txt.ToUpper, fnt, New SolidBrush(Color.White), base_rect, New StringFormat With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center})
            End If

            g.DrawRectangle(New Pen(brush_hover, 1), base_rect)
            g.DrawRectangle(New Pen(brush_down, 1), base_rect)
        End If
    End Sub
End Class
